package com.sgb.kjwl.view.ui.adapter.self.carMasters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.AddField;
import com.sgb.kjwl.model.entity.self.HistoryOfferEntity;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.ui.activity.transport.DepartureCertificateActivity;
import com.sgb.link.view.widget.ExpandedTextView;
import com.swgk.core.customview.SeeMoreView;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.UserOperating;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/4/7
 */
public class HistoricalQuoteAdapter extends BaseQuickAdapter<HistoryOfferEntity, BaseViewHolder> {
    private Context context;
    private boolean isGeneral;

    public HistoricalQuoteAdapter(Context context, List<HistoryOfferEntity> datas, boolean isGeneral) {
        super(R.layout.historical_quote_list_item, datas);
        this.context = context;
        this.isGeneral = isGeneral;
    }

    @Override
    protected void convert(BaseViewHolder holder, HistoryOfferEntity entity) {
        TextView offer_time = holder.getView(R.id.offer_time);
        TextView freight_offer = holder.getView(R.id.freight_offer);
        TextView freight_offer_all = holder.getView(R.id.freight_offer_all);

        TextView car_info = holder.getView(R.id.car_info);
        TextView car_location = holder.getView(R.id.car_location);
        TextView contact_info = holder.getView(R.id.contact_info);
        TextView contact_info_phone = holder.getView(R.id.contact_info_phone);
        RelativeLayout remark_layout = holder.getView(R.id.remark_layout);
        TextView content_view = holder.getView(R.id.content_view);
        TextView expand_view = holder.getView(R.id.expand_view);
        LinearLayout custom_content = holder.getView(R.id.custom_content);
        TextView in_time = holder.getView(R.id.arrive_time);
        TextView companyAddress = holder.getView(R.id.address);
        TextView company_arrive_time = holder.getView(R.id.company_arrive_time);
        TextView company_car_info_nub = holder.getView(R.id.company_car_info_nub);
        TextView company_contact_info = holder.getView(R.id.company_contact_info);
        TextView company_contact_info_phone = holder.getView(R.id.company_contact_info_phone);
        SeeMoreView propaganda = holder.getView(R.id.propaganda);
        TextView invoice_info = holder.getView(R.id.invoice_info);
        LinearLayout company_layout = holder.getView(R.id.company_layout);
        LinearLayout driver_layout = holder.getView(R.id.driver_layout);
        LinearLayout propaganda_layout = holder.getView(R.id.propaganda_layout);
        LinearLayout invoice_info_layout = holder.getView(R.id.invoice_info_layout);
        LinearLayout freight_offer_layout = holder.getView(R.id.freight_offer_layout);
        LinearLayout freight_offer_all_layout = holder.getView(R.id.freight_offer_all_layout);
        LinearLayout company_car_info_nub_layout = holder.getView(R.id.company_car_info_nub_layout);

        if (UserOperating.getInstance().getUserIdentity() != 0) {
            //企业,团队
            company_layout.setVisibility(View.VISIBLE);
            driver_layout.setVisibility(View.GONE);
        } else {
            //个人
            company_layout.setVisibility(View.GONE);
            driver_layout.setVisibility(View.VISIBLE);
        }

        if (entity.getCreateTime() > 0) {
            offer_time.setText(DateUtils.formatDate(String.valueOf(entity.getCreateTime()), "yyyy-MM-dd HH:mm:ss"));
        } else {
            offer_time.setText("/");
        }

        if (!TextUtils.isEmpty(entity.getFreight())) {
            freight_offer.setText("单价" + NumberUtil.trim0(entity.getFreight()) + entity.getFreightUnit());
        } else {
            freight_offer_layout.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(entity.getAllFreight())) {
            freight_offer_all.setText(NumberUtil.trim0(entity.getAllFreight()) + "元");
        } else {
            freight_offer_all_layout.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(entity.getCarNumber())) {
            company_car_info_nub.setText(entity.getCarNumber());
        } else {
            company_car_info_nub_layout.setVisibility(View.GONE);
        }

        companyAddress.setText(entity.getCurrentProvinceName() + entity.getCurrentCityName() + entity.getCurrentDistrictName() + entity.getCurrentDetailAddress());

        if (!TextUtils.isEmpty(entity.getMarketingPublicity())) {
            propaganda.setText(entity.getMarketingPublicity());
        } else {
            propaganda_layout.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(entity.getInvoiceInfoName())) {
            invoice_info.setText(entity.getInvoiceInfoName());
        } else {
            invoice_info_layout.setVisibility(View.GONE);
        }

        String inTime = DateUtils.formatDate(String.valueOf(entity.getCreateTime()), "yyyy-MM-dd");
        in_time.setText(TextUtils.isEmpty(inTime) ? "/" : inTime);
        company_arrive_time.setText(TextUtils.isEmpty(inTime) ? "/" : inTime);

        String carInfo = "";
        //车牌
        if (!TextUtils.isEmpty(entity.getCarNumber()))
            carInfo += entity.getCarNumber() + " ";
        //车型
        if (!TextUtils.isEmpty(entity.getCarTypeName()))
            carInfo += entity.getCarTypeName() + " ";
        //车长
        if (!TextUtils.isEmpty(entity.getCarLenName()))
            carInfo += entity.getCarLenName() + " ";
        //栏高0
        if (!TextUtils.isEmpty(entity.getHeightHurdle()))
            carInfo += "栏高" + NumberUtil.trim0(entity.getHeightHurdle()) + "米 ";
        //是否自卸
        if (entity.getIsDump() != null)
            if (entity.getIsDump() == 1) {
                carInfo += "自卸";
            } else {
                carInfo += "非自卸";
            }
        car_info.setText(!TextUtils.isEmpty(carInfo) ? carInfo : "/");
        String address = "";
        if (!TextUtils.isEmpty(entity.getCurrentDetailAddress()))
            address += entity.getCurrentDetailAddress();
        car_location.setText(TextUtils.isEmpty(address) ? "/" : address);

        String contactStr = "";
        if (!TextUtils.isEmpty(entity.getContrcts()))
            contactStr += entity.getContrcts();
        contact_info.setText(TextUtils.isEmpty(contactStr) ? "/" : contactStr);
        company_contact_info.setText(TextUtils.isEmpty(contactStr) ? "/" : contactStr);
        contact_info_phone.setText(TextUtils.isEmpty(entity.getContrctsNumber()) ? "" : entity.getContrctsNumber());
        company_contact_info_phone.setText(TextUtils.isEmpty(entity.getContrctsNumber()) ? "" : entity.getContrctsNumber());

        if (!TextUtils.isEmpty(entity.getNote())) {
            remark_layout.setVisibility(View.VISIBLE);
            content_view.setText(entity.getNote());
            new ExpandedTextView().showTextPick(content_view, expand_view);
        } else {
            remark_layout.setVisibility(View.GONE);
        }

        if (entity.getDiyField() != null && entity.getDiyField().size() > 0) {
            custom_content.setVisibility(View.VISIBLE);
            custom_content.removeAllViews();
            for (int i = 0; i < entity.getDiyField().size(); i++) {
                AddField itemEntity = entity.getDiyField().get(i);
                if (!TextUtils.isEmpty(itemEntity.getValue()))
                    addCustomLayout(custom_content, itemEntity);
            }
        } else {
            custom_content.setVisibility(View.GONE);
        }

        if (entity.getOfferType() == 3) {//个人

        } else if (entity.getOfferType() == 1) {//企业
            holder.getView(R.id.car_info_hint).setVisibility(View.GONE);
            holder.getView(R.id.car_info).setVisibility(View.GONE);
            holder.getView(R.id.car_location_hint).setVisibility(View.GONE);
            holder.getView(R.id.car_location).setVisibility(View.GONE);
        } else if (entity.getOfferType() == 2) {//团队
            holder.getView(R.id.car_info_hint).setVisibility(View.GONE);
            holder.getView(R.id.car_info).setVisibility(View.GONE);
            holder.getView(R.id.car_location_hint).setVisibility(View.GONE);
            holder.getView(R.id.car_location).setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(entity.getAccessory())) {
            holder.getView(R.id.annex_hint).setVisibility(View.GONE);
            holder.getView(R.id.annex_info).setVisibility(View.GONE);
        } else {
            holder.getView(R.id.annex_info).setOnClickListener(view -> {
                if (!ClickUtils.isDoubleClick2()) {
                    try {
                        List<String> images = new ArrayList<>();
                        String[] strings = entity.getAccessory().split(",");
                        Collections.addAll(images, strings);
                        DepartureCertificateActivity.start(context, images, true);
                    } catch (Exception e) {

                    }
                }
            });
        }
    }

    /**
     * 为自定义字段添加布局
     *
     * @param content
     * @param entity
     */
    private void addCustomLayout(LinearLayout content, AddField entity) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_field_layout_new, null);
        TextView remark_hint = view.findViewById(R.id.remark_hint);
        TextView content_tv = view.findViewById(R.id.content_tv);
        TextView expand_tv = view.findViewById(R.id.expand_tv);
        if (!TextUtils.isEmpty(entity.getKey())) {
            remark_hint.setText(entity.getKey());
        } else
            remark_hint.setText("自定义");

        content_tv.setText(entity.getValue());
        new ExpandedTextView().showTextPick(content_tv, expand_tv);
        content.addView(view);
    }
}
